package com.sve.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;*/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


import com.sve.Dao.ConductorMapper;

import com.sve.Dao.StationMapper;

import com.sve.Dao.TicketMapper;
import com.sve.Dao.UserMapper;
import com.sve.Entity.*;

@Controller
@RequestMapping("/Ticket")
public class TicketController {
	private StationMapper stationservice;

	public StationMapper getStationservice() {
		return stationservice;
	}

	public void setStationservice(StationMapper stationservice) {
		this.stationservice = stationservice;
	}
	

	private UserMapper usermapper;
	public UserMapper getUsermapper() {
		return usermapper;
	}
	@Autowired
	public void setUsermapper(UserMapper usermapper) {
		this.usermapper = usermapper;
	}
	
	private ConductorMapper conductormapper;
	public ConductorMapper getConductormapper() {
		return conductormapper;
	}
	@Autowired
	public void setConductormapper(ConductorMapper conductormapper) {
		this.conductormapper = conductormapper;
	}
	//��ҳ

	

	@RequestMapping("index")
	public String index(Model model){
		List<Station> stationlist=stationservice.SelectStation();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//�������ڸ�ʽ
		
		model.addAttribute("Station",stationlist);
		model.addAttribute("DD",df.format(new Date()));
		
		return "index";
	}
	
	
	private TicketMapper ticketservice;
	
	public TicketMapper getTicketservice() {
		return ticketservice;
	}
	@Autowired
	public void setTicketservice(TicketMapper ticketservice) {
		this.ticketservice = ticketservice;
	}
	
	//��̨����ҳ��
	@RequestMapping("Afters")
	public String Afters(){
		return "Afters";
	}
	//��̨��¼ҳ��
	@RequestMapping("BLogin")
	public String BLogin(){
		return "BLogin";
	}

	//����ҳ��
	@RequestMapping("indexs")
	public String indexs(Model model,String station,String time){
		//List<Station> stations=ticketService.loadStation();
		String[] weeks = {"星期天","星期一","星期二","星期三","星期四","星期五","星期六"};
		Calendar cal = Calendar.getInstance();
		int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if(week_index<0){
			week_index = 0;
		} 
		System.out.println(weeks[week_index]);
		model.addAttribute("weeks",weeks[week_index]);
		//model.addAttribute("Stations",stations);
		model.addAttribute("Station",station);
		model.addAttribute("time",time);
		return "indexs";
	}

	//��¼ҳ��
	@RequestMapping("Login")
	public String Login(){
		return "Login";
	}
	@RequestMapping("index_xz1")
	public String index_xz1(){
		return "index_xz1";
	}
	//����ҳ��
	@RequestMapping("Orders")
	public String Orders(){
		return "Orders";
	}
	//Э��ҳ��
	@RequestMapping("Protocol")
	public String Protocol(){
		return "Protocol";
	}
	//����ҳ��
	@RequestMapping("Process")
	public String Process(){
		return "Process";
	}
	//��֪ҳ��
	@RequestMapping("Notice")
	public String Notice(){
		return "Notice";
	}
	//ע��ҳ��
	@RequestMapping("Register")
	public String Register(){
		return "Register";
	}
	//�����һ�ҳ��
	@RequestMapping("passBack")
	public String passBack(){
		return "passBack";
	}
	@RequestMapping("passBack_1")
	public String passBack_1(){
		return "passBack_1";
	}
	@RequestMapping("passBack_2")
	public String passBack_2(){
		return "passBack_2";
	}
	@RequestMapping("passBack_3")
	public String passBack_3(){
		return "passBack_3";
	}
	//�û��Ļ�����Ϣ��ѯ
	@RequestMapping("/UserInfo")
	public String UserInfo(Model model,String UserName){
		//System.out.println(UserName);
		//List<User> userInfo=ticketService.loadUser(UserName);
		//model.addAttribute("UserInfo",userInfo);
		List<User> ls=usermapper.SUserAccountNumberId();
		model.addAttribute("userList", ls);
		return "UserInfo";
	}
	//·�߲�ѯ
	@RequestMapping("/BookingRoute")
	public String BookingRoute(Model model){
		//List<Route> Route=ticketService.loadRoute();
		//System.out.println("Route:"+Route.size());
		//model.addAttribute("Route", Route);
		return "BookingRoute";
	}

	//��ƱԱ��ѯ
	@RequestMapping("/ConductorInfo")
	public String Conductor(Model model,HttpServletResponse response){
		//List<Conductor> conductor=ticketService.loadConductor();
		//model.addAttribute("Conductor", conductor);
		List<Conductor> conList=conductormapper.selectConductor();
		model.addAttribute("conductorList", conList);
		return "ConductorInfo";
	}
	@RequestMapping(value="/ConductorInfoAction",method=RequestMethod.POST)
	public ModelAndView ConductorInfoAction(Model model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String name=request.getParameter("ConductorName");
		String pass=request.getParameter("ConductorPass");
		String idcard=request.getParameter("ConductorIDCard");
		String phone=request.getParameter("ConductorPhone");
		String email=request.getParameter("ConductorEmail");
		Conductor c=new Conductor();
		c.setConductorname(name);
		c.setConductorpass(pass);
		c.setConductoridcard(idcard);
		c.setConductorphone(phone);
		c.setConductoremail(email);
		int i=conductormapper.insert(c);
		
		return new ModelAndView("redirect:/Ticket/ConductorInfo");
	}
	@RequestMapping(value="/ConductorInfoUpdate",method=RequestMethod.POST)
	public ModelAndView ConductorInfoUpdate(Model model,HttpServletRequest request,HttpServletResponse response) throws IOException{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String name=request.getParameter("ConductorName");
		String pass=request.getParameter("ConductorPass");
		String idcard=request.getParameter("ConductorIDCard");
		String phone=request.getParameter("ConductorPhone");
		String email=request.getParameter("ConductorEmail");
		String conHid=request.getParameter("conHid");
		System.out.println("fdfdfdfdfd:"+conHid+"  "+name);
		Conductor c=new Conductor();
		c.setConductorid(Integer.parseInt(conHid));
		c.setConductorname(name);
		c.setConductorpass(pass);
		c.setConductoridcard(idcard);
		c.setConductorphone(phone);
		c.setConductoremail(email);
		conductormapper.updateByPrimaryKey(c);
	
		return new ModelAndView("redirect:/Ticket/ConductorInfo");
	}
	@RequestMapping(value="/ConductorInfoDelete",method=RequestMethod.POST)
	public ModelAndView ConductorInfoDelete(Model model,HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		String id=request.getParameter("hiddelete");
		//System.out.println("djdjd+"+id);
		conductormapper.deleteByPrimaryKey(Integer.parseInt(id));
		return new ModelAndView("redirect:/Ticket/ConductorInfo");
	}
	

}
